<template>
  <div class="bigbox">
    <div class="topbox">
      <el-form ref="form" label-position="left" label-width="80px">
          <el-row :gutter="20">
              <el-col :span="5">
                <el-form-item label="建筑群">
                  <FilterSelect2
                    v-model="buildingId"
                    :optionlist="qiyelist"
                    @updateModelValue="changeqiye"
                    :valuekey="{ name: 'name', value: 'id' }"
                  ></FilterSelect2>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="用能类型">
                  <el-select size="small" v-model="type" filterable clearable style="width: 100%;">
                    <el-option v-for="(item,index) in yomgnenglist" :key="index" :label="item.text" :value="item.value" class="option"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="用户">
                  <FilterSelect2
                    v-model="customerId"
                    :optionlist="yonghulist"
                    :valuekey="{ name: 'customerName', value: 'id' }"
                  ></FilterSelect2>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="余额状态">
                  <el-select size="small" v-model="balanceStatus" filterable clearable style="width: 100%;">
                    <el-option v-for="(item,index) in statuslist" :key="index" :label="item.text" :value="item.value" class="option"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="4" style="margin-top: 4px;text-align: right;">
                <el-button size="small" @click="chongzhi">重 置</el-button>
                <el-button type="primary" size="small" @click="gettablelist()">查 询</el-button>
              </el-col>
          </el-row>
      </el-form>
      <!-- <div>
        <span style="font-size: 14px;color: #606266;">园区/建筑群</span>
        <span>
          <el-select size="small" v-model="buildingId" @change="changeqiye">
            <el-option v-for="(item,index) in qiyelist" :key="index" :label="item.name" :value="item.id" class="option"></el-option>
          </el-select>
        </span>
        <span style="font-size: 14px;color: #606266;">用能类型</span>
        <span>
          <el-select size="small" v-model="type" clearable >
            <el-option v-for="(item,index) in yomgnenglist" :key="index" :label="item.text" :value="item.value" class="option"></el-option>
          </el-select>
        </span>
        <span style="font-size: 14px;color: #606266;">用户</span>
        <span>
          <el-select size="small" v-model="customerId" filterable >
            <el-option v-for="(item,index) in yonghulist" :key="index" :label="item.customerName" :value="item.id" class="option"></el-option>
          </el-select>
        </span>
        <span style="font-size: 14px;color: #606266;">余额状态</span>
        <span>
          <el-select size="small" v-model="balanceStatus" filterable clearable >
            <el-option v-for="(item,index) in statuslist" :key="index" :label="item.text" :value="item.value" class="option"></el-option>
          </el-select>
        </span>
      </div>
      <div>
        <el-button size="small" @click="chongzhi">重 置</el-button>
        <el-button type="primary" size="small" @click="gettablelist()">查 询</el-button>
      </div> -->
    </div>
    <div class="bodybox">
      <div class="title">
        <div class="butti">
          <div>用户余额</div>
          <div style="display: flex;">
            <TopTile @command="command" @daochu="daochu" @shuaxin="shuaxin"></TopTile>
          </div>
        </div>
      </div>
      <div class="table">
        <TableColumn
        ref="tabledata"
        :tableData="tableData" 
        :tableConfig="tableConfig" 
        :issize="issize">
        <el-table-column
        prop="address"
        label="余额状态">
        <template #default="{row}">
          <div class="status" style="display: flex;align-items: center;">
                        <div class="yuan" 
                        :style="{'background-color':row.balanceStatusName=='欠费'?'#e64545':row.balanceStatusName=='正常'?'#36b34b':'#f4ea2a'}"
                        style="height: 12px;width: 12px;border-radius: 50%;margin-right: 10px;"
                        ></div>
                        <div>{{ row.balanceStatusName }}</div>
                      </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="lastRechargeTime"
        align="center"
        label="最后一次充值时间">
      </el-table-column>
      <el-table-column
        prop="lastRechargeAmount"
        align="right"
        label="最后一次充值金额(元)">
      </el-table-column>
      </TableColumn>
      </div>
        <!-- 底部分页 -->
        <div class="fenye">
          <el-pagination
                background
                layout="prev, pager, next"
                :current-page.sync="pageobj.pageNo"
                :page-size="20"
                @current-change="gettablelist"
                :total="+totalCount">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {gettabledatalistapi} from '@/api/prwpaid/cost-management/Recharge'
import { getzidianxialaapi } from '@/api/guidelines'
import {gettablelistapi} from '@/api/prwpaid/user-manage/OpenAaccount'
import {getloginlistapi} from '@/api/prwpaid/file-management/Architectural-complex'
import TopTile from '@/components/TopTitle/index.vue'
import TableColumn from '@/components/Table/index.vue'
import {exportexcel} from '@/utils/excel'
export default {
  name: 'CallPoliceIndex',
  components: {
    TopTile,
    TableColumn
  },
  data() {
    return {
      balanceStatus: '',
      statuslist:[],
      customerId:'',
      buildingId:'',
      issize: 'medium',
      qiyelist: [],
      tableData: [],
      tableConfig: [
        { label: '序号', type:'index' ,center:'center',width:'80'},
        { label: '建筑群名称', prop: 'buildingsName' ,width:'200'},
        { label: '用户名称', prop: 'customerName' },
        { label: '余额(元)', prop: 'balance' ,center:'right'},
        { label: '近七日平均费用(元)', prop: 'averageAmount' ,center:'right'},
        { label: '近七日平均用量(kWh)', prop: 'averageUse',width:'200' ,center:'right'},
        { label: '预计剩余使用天数', prop: 'dayLeft' },
        // {
        //   label: '余额状态', prop: 'balanceStatusName', center: 'center',
        //   formatter: (row, column, cellValue, index) => {
        //     const text= cellValue=='待审核'?'未完成':cellValue
        //     return `<div></div>`
        //   }
        // },
        // { label: '最后一次充值时间', prop: 'lastRechargeTime' ,center:'center'},
        // { label: '最后一次充值金额(元)', prop: 'lastRechargeAmount' ,center:'right'}
      ],
      yonghulist: [],
      yomgnenglist: [],
      totalCount: '',
      pageobj: {
        pageNo: 1,
        pageSize:20
      },
      type:''
    };
  },

  async mounted() {
    const { data: { list } } = await getzidianxialaapi({ dictType: 'MBM_BALANCE_TYPE' })
    this.statuslist=list
    // this.$set(this,'balanceStatus',list[0]?.value)
    const res = await getloginlistapi()
    this.qiyelist = res.data.list
    const localbuildingId = localStorage.getItem('buildingId')
    const isBuildIdInArray = this.qiyelist.some(item => item.id == localbuildingId);
    if(isBuildIdInArray){
      this.buildingId = localbuildingId ? localbuildingId : ''
      this.getyonghuxialalist(this.buildingId)
    }else{
      this.buildingId=''
    }
    // 获取用能类型
    const yonghu = await getzidianxialaapi({ dictType: 'MBM_ENERGY_TYPE' })
    this.yomgnenglist = yonghu.data.list
    this.type = this.yomgnenglist[0].value
    this.gettablelist()
  },

  methods: {
    // 企业下拉
    changeqiye(val) {
      localStorage.setItem('buildingId', val)
      this.getyonghuxialalist(val)
    },
     // 获取用户
    async getyonghuxialalist(val) {
    const ress = await gettablelistapi({
        pageNo: 0, pageSize: 500,
      startDate: '', endDate: '', 
      state:'20',buildingId:val,customerType:''
    })
    this.yonghulist = ress.data.list
    },
    // 获取表格数据
    async gettablelist() {
      this.$refs.tabledata.loading=true
      const { data: { list, totalCount } } = await gettabledatalistapi({
        buildingId: this.buildingId, customerId: this.customerId,
        pageNo: this.pageobj.pageNo - 1, pageSize: this.pageobj.pageSize,
        type:this.type,balanceStatus:this.balanceStatus
      })
      this.tableData = list
      this.totalCount = totalCount
      this.$refs.tabledata.loading=false
    },
       // 调节表格密度
  command(val) {
      if (val === '默认') {
        this.issize = 'medium'
      } else if (val === '中等') {
        this.issize = 'small'
      } else {
        this.issize = 'mini'
      }
    },
   // 导出
   daochu() {
     exportexcel('mbm-microservice-biz/mbm/cost/balance/exportExcel', {
      buildingId: this.buildingId, customerId: this.customerId,
        type:this.type
      },'用户余额')
    },
    // 刷新列表
    shuaxin() {
      this.gettablelist()
      this.$message.success('列表刷新成功')
    },
    chongzhi() {
      localStorage.removeItem('buildingId');
      this.balanceStatus=''
      this.buildingId = ''
      this.customerId = ''
      this.type = this.yomgnenglist[0].value
      this.gettablelist()
    }
  },
};
</script>

<style lang="scss" scoped>
.option{
  padding-left: 10px;
}
.bigbox{
  height: 100%;
  padding: 10px;
  .topbox{
    height: 80px;
    padding:20px;
    background-color: #fff;
    margin-bottom: 10px;
  }
  .bodybox{
    height: calc(100% - 90px);
    background-color: #fff;
    padding: 20px;
    .butti{
      display: flex;
      justify-content: space-between;
      }
      .table{
        height: 90%;
        margin-top: 10px;
      }
      .fenye{
        text-align: right;
      }
  }
}
</style>